﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmTimKiemNhanVien : Form
    {
        public static Entities.TimKiemNhanVien nhanvien;
        Entities.TimKiemNhanVien[] hienthi;
        bool checkClose = false;

        public frmTimKiemNhanVien()
        {
            InitializeComponent();
            hienthi = Common.StaticVariable.InterfaceObject.SelecttimKiemNhanVien();
            dgvHienThi.DataSource = hienthi;
            fix();
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.Columns["MaNhanVien"].HeaderText = "Mã Nhân Viên";
            dgvHienThi.Columns["MaNhanVien"].Visible = true;
            dgvHienThi.Columns["TenNhanVien"].HeaderText = "Tên Nhân Viên";
            dgvHienThi.Columns["TenNhanVien"].Visible = true;
            dgvHienThi.Columns["DCThuongTru"].HeaderText = "Địa Chỉ";
            dgvHienThi.Columns["DCThuongTru"].Visible = true;
            if (dgvHienThi.RowCount > 0)
            {
                Common.Utilities.CountDatagridview(dgvHienThi);
                dgvHienThi.Focus();
                dgvHienThi.Select();
            }
            dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


        }

        #region Chọn
        void Chon()
        {
            try
            {
                if (nhanvien != null)
                {
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Bạn chưa chọn nhân viên", "Hệ thống cảnh báo", MessageBoxButtons.OK);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        private void Window_Loaded(object sender, EventArgs e)
        {
            rdbTimKiem1.Checked = true;
            // click chuột trái image
            btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
            btnChon.MouseDown += new MouseEventHandler(MouseDown_Chung);

            // text changed
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);

            // key up
            dgvHienThi.KeyDown += new KeyEventHandler(KeyDown_Chung);

            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);

            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);

        }

        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            for (int i1 = 0; i1 < hienthi.Length; i1++)
            {
                if (hienthi[i1].MaNhanVien == dgvHienThi.CurrentRow.Cells["MaNhanVien"].Value.ToString())
                {
                    nhanvien = hienthi[i1];  
                }
            }
            
        }
        int i = -1;

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (i < 0)
            {
                return;
            }
            if (hienthi.Length == 0)
            {
                return;
            }
            nhanvien = hienthi[i];
            checkClose = true;
            this.Close();
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            int soluong = 0;
            if (rdbTimKiem1.Checked == true)
            {
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].MaNhanVien.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        soluong++;
                    }
                }
                if (soluong == 0)
                {
                    dgvHienThi.DataSource = new Entities.TimKiemNhanVien[0];
                    //lbTongSo.Content = Common.Constants.TongSo + "0";
                    return;
                }
                Entities.TimKiemNhanVien[] hienthitheoid = new Entities.TimKiemNhanVien[soluong];
                soluong = 0;
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].MaNhanVien.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        hienthitheoid[soluong] = hienthi[i];
                        soluong++;
                    }
                }
                dgvHienThi.DataSource = hienthitheoid;
                //lbTongSo.Content = Common.Constants.TongSo + hienthitheoid.Length;

            }
            if (rdbTimKiem2.Checked == true)
            {
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].TenNhanVien.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        soluong++;
                    }
                }
                if (soluong == 0)
                {
                    dgvHienThi.DataSource = new Entities.TimKiemNhanVien[0];
                    //lbTongSo.Content = Common.Constants.TongSo + "0";
                    return;
                }
                Entities.TimKiemNhanVien[] hienthitheoma = new Entities.TimKiemNhanVien[soluong];
                soluong = 0;
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].TenNhanVien.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        hienthitheoma[soluong] = hienthi[i];
                        soluong++;
                    }
                }
                dgvHienThi.DataSource = hienthitheoma;
                //lbTongSo.Content = Common.Constants.TongSo + hienthitheoma.Length;

            }
        }

        private void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button temp = (Button)sender;

                #region chuột trái
               
                    switch (temp.Name)
                    {
                        case "btnChon":
                            {
                                Chon();
                                break;
                            }
                        case "btnThoat":
                            {
                                Thoat();
                                break;
                            }
                        default:
                            break;
                    }
                
                #endregion
            }
            catch
            {
            }
        }

        private void KeyDown_Chung(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F2)
                {
                    Chon();
                }
                if (e.KeyCode == Keys.Escape)
                {
                    Thoat();
                }
                if (e.KeyCode == Keys.Enter)
                {
                    Chon();
                }
            }
            catch
            {
            }
        }

     
    }
}
